package com.wyw.springcloud.biz;

import com.wyw.springcloud.topic.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件介绍
 *
 * @Author： Mr Wu （yewen.wu.china.gmail.com）
 * @Date: 2021/3/14 22:29
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class Controller {

    private final MyTopic produce;
    private final GroupTopic groupProduce;
    private final DelayedTopic delayedProduce;
    private final ErrorTopic errorProduce;
    private final RequeueTopic requeueProduce;
    private final DlqTopic dlqProduce;
    private final FallbackTopic fallbackProduce;



    @PostMapping("send")
    public void sendMessage(@RequestParam(value = "body") String body){
        produce.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendGroup")
    public void sendGroupMessage(@RequestParam(value = "body") String body){
        groupProduce.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendDelayedGroup")
    public void sendDelayedMessage(@RequestParam(value = "body") String body,
                                   @RequestParam(value = "seconds") Integer seconds){

        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        log.info("ready to send delayed message");
        delayedProduce.output().send(
                MessageBuilder.withPayload(messageBean)
                        .setHeader("x-delay", seconds * 1000)
                        .build());
    }

    @PostMapping("sendErrorGroup")
    public void sendErrorMessage(@RequestParam(value = "body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        errorProduce.output().send(MessageBuilder.withPayload(messageBean).build());
    }

    @PostMapping("sendrequeueGroup")
    public void sendRequeueMessage(@RequestParam(value = "body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        requeueProduce.output().send(MessageBuilder.withPayload(messageBean).build());
    }

    //死信队列
    @PostMapping("sendDlqGroup")
    public void sendDlqMessage(@RequestParam(value = "body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        dlqProduce.output().send(MessageBuilder.withPayload(messageBean).build());
    }

    //fallback升版
    @PostMapping("sendFallback")
    public void sendfbMessage(@RequestParam(value = "body") String body,
                              @RequestParam(value = "version") String version){

        //下单接口，调用不同接口
        //发到不同的队列即可
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        fallbackProduce.output().send(
                MessageBuilder.withPayload(messageBean)
                        .setHeader("version", version)
                        .build());
    }
}
